package cn.tedu._04weibo.controller;

import cn.tedu._04weibo.base.response.JsonResult;
import cn.tedu._04weibo.base.response.StatusCode;
import cn.tedu._04weibo.mapper.CommentMapper;
import cn.tedu._04weibo.pojo.dto.CommentAddParam;
import cn.tedu._04weibo.pojo.entity.Comment;
import cn.tedu._04weibo.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Slf4j
@Validated
@RestController
@RequestMapping("/v1/comment")
@Tag(name = "03-评论管理模块")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;

    @PostMapping("insert")
    @Operation(summary = "添加评论")
    @ApiOperationSupport(order = 100)
    public JsonResult insert(@RequestBody CommentAddParam commentAddParam, HttpSession session) {
        log.debug("开始处理【添加评论】的请求，参数：{}", commentAddParam);
        UserVO userVO = (UserVO) session.getAttribute("user");
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentAddParam, comment);
        comment.setUserId(userVO.getId());
        comment.setCreateTime(new Date());
        log.debug("添加评论：{}", comment);
        commentMapper.insertComment(comment);
        return JsonResult.ok();
    }

    @GetMapping("selectByWeiboId")
    @Operation(summary = "获取评论列表")
    @ApiOperationSupport(order = 200)
    public JsonResult selectByWeiboId(Long id){
        log.debug("开始处理【获取评论列表】的请求，参数：{}", id);
        return JsonResult.ok(commentMapper.selectByWeiboId(id));
    }
}